package com.zbkj.crmeb.member.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.member.dao.StoreProductLabelDao;
import com.zbkj.crmeb.member.request.StoreProductLabelRequest;
import com.zbkj.crmeb.member.service.IStoreProductLabelService;
import com.zbkj.crmeb.member.model.StoreProductLabel;
import com.zbkj.crmeb.store.model.StoreProductMer;
import com.zbkj.crmeb.store.service.StoreProductMerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品标签表 服务实现类
 * </p>
 *
 * @author xx
 * @since 2022-05-31
 */
@Service
public class StoreProductLabelServiceImpl extends ServiceImpl<StoreProductLabelDao, StoreProductLabel> implements IStoreProductLabelService {

    @Autowired
    private StoreProductMerService storeProductMerService;

    @Override
    public CommonPage<StoreProductLabel> getLabel(StoreProductLabelRequest request) {
        Page<StoreProductLabel> page = PageHelper.startPage(request.getPage(),request.getLimit());
        List<StoreProductLabel> productLabelList = this.list(Wrappers.lambdaQuery(StoreProductLabel.class)
                .and(!StringUtils.isEmpty(request.getKeywords()),i -> i.eq(NumberUtil.isInteger(request.getKeywords()), StoreProductLabel::getId ,request.getKeywords())
                        .or().like(!StringUtils.isEmpty(request.getKeywords()), StoreProductLabel::getLabelName, request.getKeywords()))
                .eq(StoreProductLabel::getMerId, request.getMerId()));
        return CommonPage.restPage(CommonPage.copyPageInfo(page, productLabelList));
    }

    @Override
    public Boolean addOrUpdLabel(StoreProductLabel storeProductLabel) {
        return this.saveOrUpdate(storeProductLabel);
    }

    @Override
    public Boolean delLabel(Integer id) {
        return this.removeById(id);
    }

    @Override
    public Boolean addProductLabel(StoreProductLabelRequest request) {
        List<StoreProductMer> productMerList = storeProductMerService.list(Wrappers.lambdaQuery(StoreProductMer.class)
                .in(StoreProductMer::getProductId,request.getProductIdList()));
        Map<Integer,StoreProductMer> storeProductMerMap = productMerList.stream().collect(Collectors.toMap(StoreProductMer::getProductId,storeProductMer -> storeProductMer));
        List<StoreProductMer> storeProductMerList = new ArrayList<>();
        request.getProductIdList().forEach(integer -> {
            StoreProductMer storeProductMer = new StoreProductMer();
            storeProductMer.setCreateTime(new Date());
            if (Objects.nonNull(storeProductMerMap.get(integer))){
                storeProductMer.setId(storeProductMerMap.get(integer).getId());
                storeProductMer.setCreateTime(null);
                storeProductMer.setUpdateTime(new Date());
            }
            storeProductMer.setProductId(integer);
            storeProductMer.setLabelId(request.getLabelId());
            storeProductMer.setLabelDisplayType(request.getLabelDisplayType());
            storeProductMerList.add(storeProductMer);
        });
        return storeProductMerService.saveOrUpdateBatch(storeProductMerList);
    }

    @Override
    public Boolean clearProductLabel(StoreProductLabelRequest request) {
        if (Objects.equals(request.getClearType(),1)){
            return storeProductMerService.update(Wrappers.lambdaUpdate(StoreProductMer.class)
                    .set(StoreProductMer::getLabelId,null)
                    .set(StoreProductMer::getLabelDisplayType,null)
                    .in(StoreProductMer::getProductId,request.getProductIdList()));
        }else {
            return storeProductMerService.update(Wrappers.lambdaUpdate(StoreProductMer.class)
                    .set(StoreProductMer::getLabelId,null)
                    .set(StoreProductMer::getLabelDisplayType,null));
        }
    }
}
